import type { Feature, LineString, GeoJsonProperties } from 'geojson'
import type { MapFeatures, SymbolFeature } from '@/map'
import { highLightProperties } from '@/map/highlights'
import { getEntityMap } from '../base'
import { type Waypoint } from '../waypoint'
import type { ATSRoute } from './types'

export function atsRoute2Feature<G extends GeoJsonProperties>(
  lst: ATSRoute,
  properties: G
): Feature<LineString, G> {
  return _atsRoute2Feature(lst, properties, getEntityMap<Waypoint>('Waypoint'))
}

function _atsRoute2Feature<G extends GeoJsonProperties>(
  lst: ATSRoute,
  properties: G,
  wpts: Record<string, Waypoint>
): Feature<LineString, G> {
  return {
    type: 'Feature',
    geometry: {
      type: 'LineString',
      coordinates: lst.waypointList.map((wId) => {
        const loc = wpts[wId].location
        return [loc.lng, loc.lat]
      })
    },
    properties
  }
}

export function highlightATSRouteFeatures(r: ATSRoute): MapFeatures {
  const wpts = getEntityMap<Waypoint>('Waypoint')
  // const symbol: SymbolFeature[] = r.waypointList.map((wptId) =>
  //   highLightWaypointFeature(wpts[wptId])
  // )
  const symbolProperty = highLightProperties.symbol.primary('')
  const symbol: SymbolFeature[] = r.waypointList.map((wptId) => {
    const { lng, lat } = wpts[wptId].location
    return {
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: [lng, lat]
      },
      properties: symbolProperty
    }
  })
  const p = highLightProperties.line.primary
  const line = [_atsRoute2Feature(r, p, wpts)]
  return { symbol, line }
  // return { line }
}
